package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.message.proto.in.AbstractXYmClubListReq;
import com.game.server.message.proto.out.XYclubinfoStruct;
import com.game.server.message.proto.out.XYmClubListRsp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class XYmClubListReq extends AbstractXYmClubListReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYmClubListRsp rsp = new XYmClubListRsp();
            UserInfoData userInfoData = DataManager.getInstance().getMapUserData().get(client.getUserId());
            Set<Integer> clubids = userInfoData.getClubids();

            List<XYclubinfoStruct> xYclubinfoStructs = new ArrayList<>();
            for (Integer cid : clubids) {
                XYclubinfoStruct xYclubinfoStruct = new XYclubinfoStruct();
                XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(cid);
                LogManager.getInstance().Log("俱乐部信息："+clubInfo.toString());
                xYclubinfoStruct.setId(clubInfo.getId());
                xYclubinfoStruct.setHeadurl(clubInfo.getHeadurl());
                xYclubinfoStruct.setXingyuvalue(clubInfo.getPlayer().get(client.getUserId()).getXingyuvalue()+"");
                xYclubinfoStruct.setAllPeopleCount(clubInfo.getPlayerCount());
                xYclubinfoStruct.setOnlinecount(clubInfo.getOnlineCount());
                xYclubinfoStruct.setContent(clubInfo.getContent());
                xYclubinfoStruct.setXingyu(clubInfo.getXingyuvalue());
                xYclubinfoStruct.setName(clubInfo.getName());
                LogManager.getInstance().Log("俱乐部信息：");
                //正在进行的牌局数  待完善------
                xYclubinfoStruct.setPaijuCount(clubInfo.findAllTable(client.getUserId(),cid).size());
                xYclubinfoStruct.setCreatorid(clubInfo.getCreatorid());
                xYclubinfoStruct.setVisitRank(0);
                if (clubInfo.getPlayer().get(client.getUserId()) != null) {
                    xYclubinfoStruct.setVisitRank(clubInfo.getPlayer().get(client.getUserId()).getIsManager());
                }
                xYclubinfoStructs.add(xYclubinfoStruct);
            }
            XYclubinfoStruct[] clubinfos = xYclubinfoStructs.toArray(new XYclubinfoStruct[0]);
            rsp.setClubinfos(clubinfos);
            client.Send(rsp);
        }
    }

}
